import React, { useReducer } from 'react'

function useReducerDome() {
    //count代表数据，dispatch代表触发方法
    const [count, dispatch] = useReducer((state, action) => {
        if (action.type === 'add') {
            return state + 1
        } else if (action.type === 'sub') {
            return state - 1
        }
        // 传进去的值代表的是初始值
    }, 0)

    return (
        <>
            <p>useReducer</p>
            <p>数据{count}</p>
            {/* 这里通过dispatch传入type，修改state里的数据 */}
            <button onClick={() => { dispatch({ type: 'add' }) }}>加</button>
            <button onClick={() => { dispatch({ type: 'sub' }) }}>减</button>
        </>
    )
}

export default useReducerDome;